package com.zlzxm.sorting.ui.fragment

import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.zlzxm.sorting.databinding.FmSortViewProductBinding
import com.zlzxm.sorting.globe.startPage
import com.zlzxm.sorting.ui.activity.AcCaoZuoProduct
import com.zlzxm.sorting.ui.view.sortview.AdapterNodeProduct
import com.zlzxm.sorting.ui.view.sortview.RootNode
import com.zlzxm.sorting.ui.view.sortview.SecondNode
import com.zlzxm.sorting.ui.vm.VmSortViewProduct
import com.zlzxm.sorting.util.ui.manager.AbsFmBase

/**
 *
 *CREATE BY zlzxm ON 2022/8/22
 *
 */
class FmSortViewProduct private constructor():AbsFmBase(), OnItemClickListener {

    companion object{

        fun create(id:String): FmSortViewProduct {

            val fragment = FmSortViewProduct()
            fragment.id = id
            return fragment
        }

    }

    val viewBinding by lazy {

        FmSortViewProductBinding.inflate(layoutInflater)
    }

    val viewModel by lazy {

        ViewModelProvider(this).get(VmSortViewProduct::class.java)
    }

    var id:String? = null

    val adapterNodeProduct = AdapterNodeProduct()

    override fun initBinding(): ViewBinding {

        return  viewBinding
    }

    override fun initViewModel() {

        viewModel.listData.observe(this,adapterNodeProduct::setNewInstance)
    }

    override fun initView() {

        viewBinding.rvProduct.layoutManager = GridLayoutManager(requireContext(),4)
        viewBinding.rvProduct.adapter = adapterNodeProduct
        adapterNodeProduct.setOnItemClickListener(this)

    }

    override fun initData() {
        viewModel.id = id
        viewModel.loadPageData()


    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

        val node = adapterNodeProduct.data[position]
        if (node is RootNode) {

            adapterNodeProduct.expandOrCollapse(position)
        } else if (node is SecondNode) {

            requireContext().startPage(AcCaoZuoProduct::class.java)
        }

    }
}